﻿using System;

namespace ReverseLinkedList
{


    public class ListNode
    {
        /// <summary>
        /// 值
        /// </summary>
        public int val;

        /// <summary>
        /// 下一个节点
        /// </summary>
        public ListNode next;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="x">初始化值</param>
        public ListNode(int x) { val = x; }
    }

    public class Solution
    {
        public ListNode ReverseList(ListNode head)
        {
            if (head == null)
                return null;

            ListNode p = null;
            while (true)
            {
                ListNode q = new ListNode(head.val);
                q.next = p;
                p = q;
                head = head.next;

                if (head == null)
                    break;
            }

            return p;
        }

        public  void PrintListNode(ListNode ln)
        {
            Console.Write("(");
            while (ln != null)
            {
                Console.Write(ln.val);

                if (ln.next != null) Console.Write("->");

                ln = ln.next;
            }

            Console.Write(")");
        }
    }


}